package com.slliver.orderservice.controller;


import com.slliver.commonmodel.order.Order;
import com.slliver.commonmodel.product.Product;
import com.slliver.dubboservice.api.ProductServiceApi;
import com.slliver.orderservice.feign.ProductFeignService;
import com.slliver.orderservice.service.OrderService;
//import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // http://192.168.3.81:9002/order/saveOrderByRestTemplate?userId=1001&productId=1001
    @GetMapping("/saveOrderByRestTemplate")
    public Order saveOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId){
        return orderService.saveOrderByRestTemplate(userId, productId);
    }

    /**
     * 进阶创建订单改进版本，使用编程式的负载均衡
     */
    // http://192.168.3.81:9002/order/saveOrderWithLoadBalancerByRestTemplate?userId=1001&productId=1001
    @GetMapping("/saveOrderWithLoadBalancerByRestTemplate")
    public Order saveOrderWithLoadBalancerByRestTemplate(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId){
        return orderService.saveOrderWithLoadBalancerByRestTemplate(userId, productId);
    }

    /**
     * 再次进阶创建订单改进版本，使用注解式的负载均衡
     */
    // http://192.168.3.81:9002/order/saveOrderWithAnnotationLoadBalancerByRestTemplate?userId=1001&productId=1001
    @GetMapping("/saveOrderWithAnnotationLoadBalancerByRestTemplate")
    public Order saveOrderWithAnnotationLoadBalancerByRestTemplate(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId){
        return orderService.saveOrderWithAnnotationLoadBalancerByRestTemplate(userId, productId);
    }

    // http://localhost:9001/order/saveOrderByOpenFeign?userId=1001&productId=1001
    @GetMapping("/saveOrderByOpenFeign")
    public Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId){
        return orderService.saveOrderByOpenFeign(userId, productId);
    }

    // http://localhost:8083/order/saveOrderByDubbo?userId=1001&productId=1001
    @GetMapping("/saveOrderByDubbo")
    public Order saveOrderByDubbo(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId){
        return orderService.saveOrderByDubbo(userId, productId);
    }


    @Autowired
    private ProductFeignService productFeignService;

    @GetMapping("/getProductByOpenFeign/{productId}")
    public String getProductByOpenFeign(@PathVariable("productId") Long productId){
        Product product = productFeignService.getProduct(productId);
        return product.toString();
    }

    /**
    @Reference(version="1.0.0")
    private ProductServiceApi productServiceApi;


    @GetMapping("/getProductByDubbo/{productId}")
    public String getProductByDubbo(@PathVariable("productId") Long productId){
        Product product = productServiceApi.getProductById(productId);
        return product.toString();
    }
    **/
}
